﻿/******************************************************************************
 * 【本类功能概述】                                 					      *
 *  版权所有（C）2017-20XX，米兔网络                                         *
 *  保留所有权利。                                                            *
 ******************************************************************************
 *  作者 : <hughjk>
 *  创建时间:2019年5月7日 16:55:46
 *  文件描述:能力构建类
 *****************************************************************************/

using System.Collections.Generic;
using UnityEngine;

public abstract class BuildAbilitys
{
    public BuildAbilitysData m_BuildAbilitysData;
    public Dictionary<int, Ability> m_Abilitys = new Dictionary<int, Ability>();
    public List<Ability> m_AbilitysList = new List<Ability>();
    public Transform m_transform;
    public bool isBuild = false;
    public AbilityTool m_Tool;

    public virtual void InitData(BuildAbilitysData m_Data, AbilityTool tool)
    {
        m_BuildAbilitysData = m_Data;
        m_Tool = tool;
    }

    //构建能力
    public virtual void Build(Transform transform)
    {
        m_transform = transform;
        isBuild = true;
        m_Abilitys.Clear();
        var de = m_BuildAbilitysData.m_AbilitysData.GetEnumerator();
        while (de.MoveNext())
        {
            var cur = de.Current;
            if (cur.Value != null && cur.Value.m_isBuildCom == false)//是否需要构建组件
            {
                continue;
            }
            Ability ability;
            if (m_Abilitys.TryGetValue(cur.Key, out ability))
            {
                if (cur.Value != null)
                {
                    ability.InitData(cur.Value);
                }
            }
            else
            {
                ability = m_Tool.Acquire(cur.Key);
                if (cur.Value != null)
                {
                    ability.InitData(cur.Value);
                }
                m_Abilitys.Add(cur.Key, ability);
            }
        }
        m_AbilitysList = new List<Ability>(m_Abilitys.Values);
        int Num = m_AbilitysList.Count;
        for (int i = 0; i < Num; i++)
        {
            m_AbilitysList[i].Init(this);
        }
        for (int i = 0; i < Num; i++)
        {
            m_AbilitysList[i].GetAbility(this);
        }
    }

    //失去能力
    public virtual void LoseAbility()
    {
        int Num = m_AbilitysList.Count;
        for (int i = 0; i < Num; i++)
        {
            m_AbilitysList[i].Over();
        }
    }

    //数据刷新
    public virtual void OnUpdate()
    {
        if (!isBuild)
        {
            return;
        }
        int Num = m_AbilitysList.Count;
        for (int i = 0; i < Num; i++)
        {
            m_AbilitysList[i].OnUpdata();
        }
    }

    //重置能力
    public virtual void OnReset()
    {
        int Num = m_AbilitysList.Count;
        for (int i = 0; i < Num; i++)
        {
            m_AbilitysList[i].OnReset();
        }
    }

    //获取能力
    public virtual T FinAbility<T>(int types) where T : Ability
    {
        Ability ability;
        if (m_Abilitys.TryGetValue(types, out ability))
        {
            return ability as T;
        }
        return default(T);
    }

    //获取能力
    public virtual T FinAbilityData<T>(int types) where T : AbilityData
    {
        AbilityData ability;
        if (m_BuildAbilitysData.m_AbilitysData.TryGetValue(types, out ability))
        {
            return ability as T;
        }
        return default(T);
    }

    //触发能力
    public virtual void OnTrigger(BuildAbilitys entity)
    {
        var e = m_Abilitys.GetEnumerator();
        while (e.MoveNext())
        {
            e.Current.Value.OnTrigger(entity);
        }
    }

    //执行能力
    public virtual void OnDispose(BuildAbilitys entity)
    {
        var e = m_Abilitys.GetEnumerator();
        while (e.MoveNext())
        {
            e.Current.Value.OnDispose(entity);
        }
    }
}